package com.topcheer.provider.rbac.rest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.topcheer.common.auth.context.BaseContextHandler;
import com.topcheer.provider.rbac.entity.Role;
import com.topcheer.provider.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.topcheer.provider.rbac.entity.Menu;
import com.topcheer.provider.rbac.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageInfo;
import com.topcheer.common.base.util.JsonMapper;
import com.topcheer.common.base.vo.BaseResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/menu")
@Api(description = "菜单信息")
public class MenuController{

    @Autowired
    MenuService menuService;

    @Autowired
    RoleService roleService;

    @PostMapping("")
    @ApiOperation(value="新增菜单", notes="新增菜单", produces="application/json")
    public BaseResponse<Menu> insert(@RequestBody Menu entity){
        menuService.insert(entity);
        return new BaseResponse<Menu>().code(HttpStatus.OK.value()).msg(HttpStatus.OK.getReasonPhrase());
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据id获取菜单", notes="根据id获取菜单", produces="application/json")
    public BaseResponse<Menu> selectOne(@PathVariable("id") Long id){
        Object o = menuService.selectOne(id);
        return new BaseResponse<Menu>().data((Menu) o);
    }

    @GetMapping("/page")
    @ApiOperation(value="获取菜单分页信息", notes="获取菜单分页信息", produces="application/json")
    public BaseResponse<PageInfo> page(String page){
        JsonMapper jsonMapper = JsonMapper.defaultMapper();
        Map map = jsonMapper.fromJson(page, Map.class);
        PageInfo pageInfo = menuService.selectPage(map);
        return new BaseResponse<PageInfo>().data(pageInfo);
    }

    @PutMapping("")
    @ApiOperation(value="更新菜单", notes="更新菜单", produces="application/json")
    public BaseResponse<Menu> update(@RequestBody Menu entity){
        menuService.update(entity);
        return new BaseResponse<Menu>();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="删除菜单", notes="删除菜单", produces="application/json")
    public BaseResponse<Menu> delete(@PathVariable("id") Long id){
        menuService.deleteById(id);
        return new BaseResponse<Menu>();
    }

    @GetMapping("/all")
    @ApiOperation(value="获取所有菜单", notes="获取所有菜单", produces="application/json")
    public BaseResponse<List> selectAll(){
        List list = menuService.selectAll();
        return new BaseResponse<List>().data(list);
    }

    @GetMapping("/user")
    @ApiOperation(value="获取用户菜单", notes="获取用户菜单", produces="application/json")
    public BaseResponse<List> selectUserMenu(){
        List<Role> roleList = roleService.selectRoleByUserId(BaseContextHandler.getUserID());
        List<Long> list = roleList.stream().map(role -> role.getId()).collect(Collectors.toList());
        List<Menu> menus = menuService.selectMenuByRole(list);
        return new BaseResponse<List>().data(menus);
    }
}
